package com.fly.common;

import java.util.LinkedHashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Author: D.Yang Email: koyangslash@gmail.com Date: 16/10/9 Time: 下午1:37 Describe: 基础控制器
 */
public class BaseController
{
    protected Logger log = LoggerFactory.getLogger(getClass());
    
    /**
     * 返回调用失败（带消息）<BR>
     * success字段是通信标识，非业务标识<BR>
     * 业务是否成功需要查看resultCode来判断
     * 
     * @param msg 需要返回的消息
     * @return result
     */
    protected Map<String, Object> renderError(String msg)
    {
        Map<String, Object> result = new LinkedHashMap<String, Object>();
        result.put("success", false);
        result.put("msg", msg);
        return result;
    }
    
    /**
     * 业务处理失败（带消息）
     * 
     * @param resultCode 业务结果码
     * @param msg 需要返回的消息
     * @return result
     */
    protected Map<String, Object> renderFail(String resultCode, String msg)
    {
        Map<String, Object> result = new LinkedHashMap<String, Object>();
        result.put("success", true);
        if (!"0000".equals(resultCode))
        {
            result.put("resultCode", resultCode);
        }
        result.put("msg", msg);
        return result;
    }
    
    /**
     * 业务处理成功
     * 
     * @return result
     */
    protected Map<String, Object> renderSuccess()
    {
        Map<String, Object> result = new LinkedHashMap<String, Object>();
        result.put("success", true);
        result.put("resultCode", "0000");
        return result;
    }
    
    /**
     * 业务处理成功（带信息）
     * 
     * @param msg 需要返回的信息
     * @return result
     */
    protected Map<String, Object> renderSuccess(String msg)
    {
        Map<String, Object> result = renderSuccess();
        result.put("msg", msg);
        return result;
    }
    
    /**
     * 业务处理成功（带数据）
     * 
     * @param obj 需要返回的数据对象
     * @return result
     */
    protected Map<String, Object> renderSuccess(Object obj)
    {
        Map<String, Object> result = new LinkedHashMap<String, Object>();
        result.put("success", true);
        result.put("resultCode", "0000");
        result.put("data", obj);
        return result;
    }
}
